<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <div class="card-header">
          <img src="/src/static/登录logo.png-removebg-preview.png" alt="">
          <p class="subtitle">注册新账号，开启您的二手交易之旅</p>
        </div>
      </template>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="registerForm"
        label-width="auto"
        class="register-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            type="text"
            placeholder="请输入用户名/学号"
            prefix-icon="User"
            clearable
            @keyup.enter.native="register"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            @keyup.enter.native="register"
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="ruleForm.confirmPassword"
            type="password"
            placeholder="请确认密码"
            prefix-icon="Lock"
            show-password
            @keyup.enter.native="register"
          />
        </el-form-item>
        <el-form-item prop="email">
          <el-input
            v-model="ruleForm.email"
            type="email"
            placeholder="请输入邮箱"
            prefix-icon="Mail"
            clearable
            @keyup.enter.native="register"
          />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-radio-group v-model="ruleForm.role">
            <el-radio value="管理员">管理员</el-radio>
            <el-radio value="用户">用户</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="register"
            :loading="loading"
            class="register-btn"
          >
            注册
          </el-button>
        </el-form-item>
        <div class="footer-links">
          <el-link type="info" @click="goLogin">已有账号，去登录</el-link>
        </div>
      </el-form>
    </el-card>
    <div class="copyright">
      ©2025 校园二手交易平台 - 让闲置物品流动起来
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

const router = useRouter();
const registerForm = ref(null);
const loading = ref(false);

const ruleForm = ref({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  role: '用户'
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value!== ruleForm.value.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      }, trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
};

const register = () => {
  registerForm.value.validate((valid) => {
    if (!valid) return;
    loading.value = true;
    // 模拟注册API请求
    setTimeout(() => {
      // 这里应是实际后端注册逻辑，成功后跳转并存储信息
      ElMessage.success('注册成功');
      localStorage.setItem("token", "mock-register-token");
      localStorage.setItem("user", JSON.stringify({
        username: ruleForm.value.username,
        role: ruleForm.value.role
      }));
      router.push({ path: "/index" });
      loading.value = false;
    }, 800);
  });
};

const goLogin = () => {
  ElMessage.info('跳转到登录页面');
  router.push({ path: "/login" });
};
</script>

<style scoped>
.register-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.register-card {
  width: 100%;
  max-width: 420px;
  border-radius: 12px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
  border: none;
}

.card-header {
  text-align: center;
  padding: 10px 0;
}

.card-header h2 {
  margin: 0;
  color: #409EFF;
  font-size: 24px;
}

.subtitle {
  margin: 5px 0 0;
  color: #909399;
  font-size: 14px;
}

.register-form {
  padding: 20px 30px;
}

.el-form-item {
  margin-bottom: 22px;
}

.register-btn {
  width: 100%;
  height: 42px;
  font-size: 16px;
  letter-spacing: 2px;
}

.footer-links {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
}

.copyright {
  margin-top: 30px;
  color: #909399;
  font-size: 12px;
  text-align: center;
}

/* 响应式调整 */
@media (max-width: 480px) {
.register-card {
    margin-top: 0;
    box-shadow: none;
  }
  
.register-form {
    padding: 20px 15px;
  }
}
</style>